package com.zhejiangzhengyuan.municipal_common.utils.LED;

import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HeartBeatUtils {


    /**
     * 将传进来的 Document 解析
     *
     * @param document
     * @return
     */
    public static List<Map<String, String>> GetHeartBeatData(Document document) {
        if (document == null) {
            System.out.println("document 数据为空");
            return null;
        }
        Elements trs = document.select("table").select("tr");
        List<Map<String, String>> strMap = DataParse(trs.eachText());
        return strMap;
    }


    /**
     * 返回心跳数据
     *
     * @param eachText
     * @return
     */
    public static List<Map<String, String>> DataParse(List<String> eachText) {
        if (eachText != null && eachText.size() > 0) {
            List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
            String[] split = eachText.toString().split(",");
            for (int i = 0; i < split.length; i++) {
                    if (split[i].contains("设备") || split[i].contains("心跳")) {  //todo 含有中文的直接跳过
                        continue;
                    }
                Map<String, String> map = new HashMap<String, String>();
                String[] s = split[i].split(" ");//todo 以空格分割]
                if (s.length == 5) {
                    map.put("MAC", s[1]);
                    map.put("DeviceCode", s[2]);
                    map.put("DATE", s[3]);
                    map.put("Time", s[4]);

                    mapList.add(map);//todo  添加到集合list
                }
            }
            return mapList;
        }
        return null;
    }
}
